package com.example.job.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.job.mapper.SysJobMapper;
import com.example.job.po.SysJob;
import com.example.job.quartz.Job;
import com.example.job.utils.CronExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author chentao
 * @version 1.0
 * @description: TODO
 * @date 2025/3/26 下午11:04
 */
@Service
public class SysJobServiceImpl extends ServiceImpl<SysJobMapper, SysJob> implements SysJobService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public List<SysJob> selectJobList(Date runTime) {
        return getBaseMapper().selectJobList(runTime);
    }

    @Override
    public Date selectNextRunTime(Date time) {
        List<SysJob> list = getBaseMapper().selectJobListBeyondRunTime(time);
        if (list.isEmpty()) {
            return DateUtil.offsetMinute(new Date(), 10);
        }
        return getBaseMapper().selectNextRunTime(time);
    }

    /***
     * 添加一个定时任务
     * @param name
     * @param minute
     * @param job
     * @return
     * @throws Exception
     */
    @Override
    public SysJob addJob(String name, int minute, Job job, boolean autoDelete, String context) throws Exception {
        Date runTime = DateUtil.offsetMinute(new Date(), minute);
        String cron = getCron(runTime);
        return this.addJob(name, cron, job, runTime, autoDelete, context);
    }

    /***
     * 新增一个定时任务
     * @param name
     * @param cron
     * @param job
     * @return
     * @throws Exception
     */
    @Override
    public SysJob addJob(String name, String cron, Job job, Date nextTime, boolean autoDelete, String context) throws Exception {
        if (nextTime == null) {
            CronExpression cronExpression = new CronExpression(cron);
            nextTime = cronExpression.getNextValidTimeAfter(new Date());
        }
        SysJob sysJob = new SysJob();
        sysJob.setName(name);
        sysJob.setStatus(true);
        sysJob.setJobClass(job.getClass().getName());
        sysJob.setCron(cron);
        sysJob.setExtraParameter(context);
        sysJob.setAutoDelete(autoDelete);
        sysJob.setNextRunTime(nextTime);
        sysJob.recalculateFields();
        this.save(sysJob);
        return sysJob;
    }

    /***
     * 更新改变定时任务
     * @param sysJob
     * @throws Exception
     */
    @Override
    public void updateJob(SysJob sysJob) throws Exception {
        sysJob.recalculateFields();
        this.saveOrUpdate(sysJob);
    }

    /***
     * 取消定时任务
     * @param id
     * @throws Exception
     */
    @Override
    public void cancelJob(Long id) throws Exception {
        SysJob sysJob = getById(id);
        sysJob.setStatus(false);
        sysJob.setCanceled(true);
        this.updateJob(sysJob);
    }

    @Override
    public String getCron(Date date) {
        StringBuffer cronExpression = new StringBuffer();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        cronExpression.append(calendar.get(Calendar.SECOND) + " ").append(calendar.get(Calendar.MINUTE) + " ").append(calendar.get(Calendar.HOUR_OF_DAY) + " ").append(calendar.get(Calendar.DATE) + " ").append((calendar.get(Calendar.MONTH) + 1) + " ").append("?" + " ").append(calendar.get(Calendar.YEAR));
        return cronExpression.toString();
    }
}
